/*------------------利用cookie记录用户点击习惯-----------------*/
var order = 0;
$(".icons-content ul>li a").on('click', function() {
    var $id = $(this).attr('id');
    if (getCookie($id)) {
        var objStr = getCookie($id);
        var Obj = JSON.parse(objStr);
        var times = Number(Obj.c);
        times = times + 1;
        Obj.c = times;
        var jsonStr = JSON.stringify(Obj);
        setCookie($id, jsonStr, 365);
    } else {
        var c = 1;
        order = order + 1;
        var obj = { c: 1, o: order };
        var jsonObj = JSON.stringify(obj);
        setCookie($id, jsonObj, 365);
    }

    var cookieStr = document.cookie;
    var cookieArr = cookieStr.split(";");
    var orderCookie = bubbleSort(cookieArr);
    var len = orderCookie.length;
    if(len > 5) {
    	var oldCookie = orderCookie[0];
    	var cookieName = oldCookie.split("=")[0];
    	cookieName = $.trim(cookieName);
    	delCookie(cookieName);
    	orderCookie.shift();
    }
    
});

function bubbleSort(arr) {
    var len = arr.length;
    for (var i = 0; i < len; i++) {
        for (var j = 0; j < len - 1 - i; j++) {
            var itemObj1 = JSON.parse(arr[j].split("=")[1]);
            var item1 = itemObj1.o;
            var itemObj2 = JSON.parse(arr[j + 1].split("=")[1]);
            var item2 = itemObj2.o;
            if (item1 > item2) {
                var temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}
/*--------------操作cookie的方法--------------*/
//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); }
    }
    return "";
}
//删除cookie
function delCookie(name) {
    setCookie(name, ' ', -1)
}